package servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import decoder.ChineseDecoder;

import model.Member;
import factory.DaoFactory;

/**
 * Servlet implementation class ModifyMemberMessageServlet
 */
@WebServlet("/ModifyMemberMessageServlet")
public class ModifyMemberMessageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ModifyMemberMessageServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String name = ChineseDecoder.getUtf8FromIso8859_1(request.getParameter("name"));
		Member member = DaoFactory.getMemberDao().find(name);
		request.setAttribute("member", member);
		String address ="/jsp/ModifyMemberMessage.jsp?name=" + name;
		RequestDispatcher dispatcher =request.getRequestDispatcher(address);
		dispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String name = ChineseDecoder.getUtf8FromIso8859_1(request.getParameter("name"));
		String password = ChineseDecoder.getUtf8FromIso8859_1(request.getParameter("password"));
		String password_again = ChineseDecoder.getUtf8FromIso8859_1(request.getParameter("password_again"));
		String gender = ChineseDecoder.getUtf8FromIso8859_1(request.getParameter("gender"));

		int age = 0;
		try {
			age = Integer.parseInt(request.getParameter("age"));
		} catch (NumberFormatException e) {
			response.sendRedirect(request.getContextPath() + "/jsp/ErrorPage.jsp?error=MdfMbrMsgAgeError&name=" + name);
			return;
		}
		
		if (age <= 0) {
			response.sendRedirect(request.getContextPath() + "/jsp/ErrorPage.jsp?error=MdfMbrMsgAgeError&name=" + name);
			return;
		}

		String address = ChineseDecoder.getUtf8FromIso8859_1(request.getParameter("address"));

		if (password.equals(password_again)) {
			Member member = new Member(name, password, gender, age, address, -1, null, 0);

			boolean success = DaoFactory.getMemberDao().updateMessageByName(member);
			if (success) {
				request.setAttribute("member", member);
				String add ="/jsp/MemberMessageManagement.jsp";
				RequestDispatcher dispatcher =request.getRequestDispatcher(add);
				dispatcher.forward(request, response);
			} else {
				response.sendRedirect(request.getContextPath() + "/jsp/ErrorPage.jsp?error=MdfMbrMsgDatabaseError&name=" + name);
			}
		} else {
			response.sendRedirect(request.getContextPath() + "/jsp/ErrorPage.jsp?error=MdfMbrMsgUnmatchPassword&name=" + name);
		}

	}

}
